define("util/tool", [
    "leaflet",
    "core/namespace"

], function (L) {


    L.ICT.Util.Tool = L.Class.extend({

        initialize: function () {

        },

        /**
         *有约束区域拖拽
         *@method _drag
         *@param _body {String} 待移动元素id 父元素
         *@param _pannel {String} 约束元素id 子元素
         */
        drag: function (_body, _pannel) {
            var dragging = false;
            var iX, iY;
            var pannel = $('#' + _pannel);
            var body = $('#' + _body);
            var uKey = "drag_" + L.stamp(body);

            //pannel.css("cursor", "move");
            pannel.data("drag", "drag");
            pannel.data("panel", uKey);
            body.addClass(uKey);

            if(!this.initDrag) {
                this.initDrag = true;

                $(document).mousedown(function (e) {
                    body = null;
                    pannel = null;

                    pannel = $(e.target);
                    pannel = pannel.data("drag") ? pannel : pannel.parent();
                    if(pannel.data("drag") && pannel.data("drag") == "drag" && pannel.data("panel")) {} else {
                        return;
                    }
                    dragging = true;

                    body = pannel.closest('.' + pannel.data("panel"));
                    if(body.length == 0) return;

                    body = body[0];

                    iX = e.clientX;
                    iY = e.clientY;

                    var point = L.DomUtil.getPosition(body);
                    if(point) {
                        iX = iX - point.x;
                        iY = iY - point.y;
                    }
                    //body.setCapture && body.setCapture();
                    return false;
                });

                $(document).mousemove(function (e) {
                    if(dragging && body) {
                        var e = e || window.event;
                        var oX = e.clientX - iX;
                        var oY = e.clientY - iY;

                        var point = L.point(oX, oY);
                        L.DomUtil.setPosition(body, point);
                        return false;
                    }
                });

                $(document).mouseup(function (e) {
                    //var se = document.getElementById(_body);
                    dragging = false;
                    iX = null;
                    iY = null;
                    pannel = null;
                    body = null;
                    uKey = null;
                    //  se.releaseCapture;
                    //e.cancelBubble = true;
                });
            }

        },

        /*经纬度转化为度分秒
         *@val Number 经纬度数值  经度范围：-180——180 纬度范围：-90——90
         *@latlngtype String 数值类型 lat-纬度 lng-经度
         *@iskeepCapacity 是否保证经纬度的位数 默认为是
         *@spointCount Number option 秒保留的小数点位数 没指定默认为整数
         *示例1： L.ict.util.latlngTodfm(37.534,'lat') ——> [37, 32, 32, "N"]
         *示例2： L.ict.util.latlngTodfm(117.128,'lng',2) ——> [117, 7, "40.80", "E"]
         */
        latlngTodfm: function (val, latlngtype, iskeepCapacity, spointCount) {
            var resobj = [],
                dir;
            newval = parseFloat(val);
            var positiveNum = Math.abs(newval);
            var du = parseInt(positiveNum);
            var fen = parseInt(positiveNum * 60 - du * 60);
            var miao = positiveNum * 60 * 60 - fen * 60 - du * 60 * 60;
            if(typeof spointCount === 'undefined' || spointCount <= 0) { spointCount = 0; }
            // if(spointCount === 0) {
            //     miao = parseInt(miao);
            // } else {
            //     miao = miao.toFixed(spointCount);
            // }

            // 四舍五入
            miao = parseFloat(miao.toFixed(spointCount));
            if(miao >= 60){
                miao = 0;
                miao = miao.toFixed(spointCount);
                fen += 1;
                if(fen >=60){
                    fen = 0;
                    du += 1;
                }
            }
            if(typeof iskeepCapacity === 'undefined') { iskeepCapacity = true; }
            if(iskeepCapacity) {
                if(latlngtype === 'lng') {
                    if(du < 10) du = '00' + du;
                    if(du >= 10 && du < 100) du = '0' + du;
                } else {
                    if(du < 10) du = '0' + du;
                }
                if(fen < 10) fen = '0' + fen;
                if(miao < 10 && spointCount === 0) miao = '0' + miao;

            }
            if(latlngtype === 'lat') {
                if(newval < 0) dir = '南纬';
                else dir = '北纬';
            }
            if(latlngtype === 'lng') {
                if(newval < 0) dir = '西经';
                else dir = '东经';
            }
            resobj.push(du, fen, miao, dir);
            return resobj;
        },

        //返回英文方向
        latlngTodfm2: function (val, latlngtype, iskeepCapacity, spointCount) {
            var resobj = [],
                dir;
            newval = parseFloat(val);
            var positiveNum = Math.abs(newval);
            var du = parseInt(positiveNum);
            var fen = parseInt(positiveNum * 60 - du * 60);
            var miao = positiveNum * 60 * 60 - fen * 60 - du * 60 * 60;
            if(typeof spointCount === 'undefined' || spointCount <= 0) { spointCount = 0; }
            // if(spointCount === 0) {
            //     miao = parseInt(miao);
            // } else {
            //     miao = miao.toFixed(spointCount);
            // }

            // 四舍五入
            miao = parseFloat(miao.toFixed(spointCount));
            if(miao >= 60){
                miao = 0;
                miao = miao.toFixed(spointCount);
                fen += 1;
                if(fen >=60){
                    fen = 0;
                    du += 1;
                }
            }
            if(typeof iskeepCapacity === 'undefined') { iskeepCapacity = true; }
            if(iskeepCapacity) {
                if(latlngtype === 'lng') {
                    if(du < 10) du = '00' + du;
                    if(du >= 10 && du < 100) du = '0' + du;
                } else {
                    if(du < 10) du = '0' + du;
                }
                // if(fen<10) fen = '0'+fen;
                // if(miao<10 && spointCount===0) miao = '0'+miao;

            }
            if(latlngtype === 'lat') {
                if(newval < 0) dir = 'S';
                else dir = 'N';
            }
            if(latlngtype === 'lng') {
                if(newval < 0) dir = 'W';
                else dir = 'E';
            }
            resobj.push(du, fen, miao, dir);
            return resobj;
        },

        latlngTodfmStr: function (val, latlngtype, spointCount) {
            var res = this.latlngTodfm(val, latlngtype, spointCount);
            var str = res[3] + " " + res[0] + "°" + res[1] + "′" + res[2] + "″";
            return str;

        },

        /*
         * 参数说明：
         * number：要格式化的数字
         * decimals：保留几位小数
         * dec_point：小数点符号
         * thousands_sep：千分位符号
         * roundtag:舍入参数，默认 "ceil" 向上取,"floor"向下取,"round" 四舍五入
         * */
        number_format: function (number, decimals, dec_point, thousands_sep, roundtag) {

            number = (number + '').replace(/[^0-9+-Ee.]/g, '');
            roundtag = roundtag || "ceil"; //"ceil","floor","round"
            var n = !isFinite(+number) ? 0 : +number,
                prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
                sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
                dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
                s = '',
                toFixedFix = function (n, prec) {
                    var k = Math.pow(10, prec);
                    console.log();
                    return '' + parseFloat(Math[roundtag](parseFloat((n * k).toFixed(prec * 2))).toFixed(prec * 2)) / k;
                };
            s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
            var re = /(-?\d+)(\d{3})/;
            while(re.test(s[0])) {
                s[0] = s[0].replace(re, "$1" + sep + "$2");
            }
            if((s[1] || '').length < prec) {
                s[1] = s[1] || '';
                s[1] += new Array(prec - s[1].length + 1).join('0');
            }
            return s.join(dec);
        },

        /*四舍五入，保留位数为roundDigit*/
        formatStr: function (numberRound, roundDigit) {
            if(numberRound.indexOf(".") >= 0) {
                var alllen = numberRound.length;
                var lencz = alllen - numberRound.indexOf(".");
                var xswall = lencz - 1;
                if(xswall <= roundDigit) {
                    return numberRound;
                } else {
                    var indexend = numberRound.indexOf(".") + roundDigit + 1;
                    return numberRound.substr(0, indexend);
                }
            } else {
                return numberRound;
            }

        },

        /*经度整数部分始终显示三位数*/
        formatLng: function (lng) {

            return lng < 10 ? '00' + lng : (lng < 100 ? '0' + lng : lng);

        },

        /*纬度整数部分始终显示两位数*/
        formatLat: function (lat) {

            return lat < 10 ? '0' + lat : lat;

        },

        /*米转化为海里*/
        convertMileToNmile: function (mileVal) {
            var oval = parseFloat(mileVal);
            var newval = oval / (1.852 * 1000);
            return newval.toFixed(3);
        },

        /*海里转化为米*/
        convertNmileToMile: function (nmileVal) {
            var oval = parseFloat(nmileVal);
            var newval = oval * 1.852 * 1000;
            return newval.toFixed(3);
        },

        getRoundNum: function (num) {
            var pow10 = Math.pow(10, (Math.floor(num) + '').length - 1),
                d = num / pow10;

            d = d >= 10 ? 10 :
                d >= 5 ? 5 :
                d >= 3 ? 3 :
                d >= 2 ? 2 : 1;

            return pow10 * d;
        },

        /*
         * 解析url地址栏参数
         */
        parseQueryString: function (url) {
            var obj = {};
            var keyvalue = [];
            var key = "",
                value = "";
            var paraString = url.substring(url.indexOf("?") + 1, url.length).split("&");
            for(var i in paraString) {
                if(paraString.hasOwnProperty(i)) {
                    keyvalue = paraString[i].split("=");
                    key = keyvalue[0];
                    value = keyvalue[1];
                    obj[key] = value;
                }
            }
            return obj;
        },

        /*
         * 安排函数f()在未来的调用模式
         * 在等待了若干毫秒之后调用f()
         * 如果设置了interval并没有设置end参数，则对f()调用将不会停止
         * 如果没有设置interval和end，只在若干毫秒后调用f一次
         * 只有指定了f,才会从start=0的时刻开始
         * 注意，调用invoke不会阻塞，它会立即返回
         */
        invoke: function (f, start, interval, end) {
            if(!start) start = 0; //默认设置为0 毫秒
            if(arguments.length <= 2)
                setTimeout(f, start); //若干毫秒后的单次调用模式
            else {
                setTimeout(repeat, start); //在若干毫秒后调用repeat()
                function repeat() {
                    var h = setInterval(f, interval); //循环调用f()
                    //在end毫秒后停止调用
                    if(end) setTimeout(function () { clearInterval(h); }, end);
                }
            }

        },
        /* *
        解析各类区域的geojson数据并转换成leaflet图层
         */
        convertGeoJsonToLeafletLayer: function (geoFeature){
          var layer = null;
          var layerType = geoFeature.geometry.type;
          var cors = geoFeature.geometry.coordinates;
          var propers = geoFeature.properties;
          if (layerType === 'Circular') {
            var latlng = L.latLng(cors[0][1], cors[0][0]);
            var radius = propers.radius;
            layer = L.circle(latlng, radius);
          } else if (layerType === 'Rectangle'){
            var bounds = L.latLngBounds(L.latLng(cors[0][1], cors[0][0]), L.latLng(cors[1][1], cors[1][0]));
            layer = L.rectangle(bounds);
          } else if (layerType === 'Polygon'){
            var latlngs = [];
            for (var i = 0, len = cors.length; i < len; i++){
              latlngs.push(L.latLng(cors[i][1], cors[i][0]));
            }
            layer = L.polygon(latlngs);
          } else if (layerType === 'Sector'){
            var latlng = L.latLng(cors[0][1], cors[0][0]);
            var sdeg = propers.start_deg;
            var edeg = propers.end_deg;
            var sradius = propers.radsmal;
            var eradius = propers.radius;
            layer = L.sector(latlng, {
              renderer: L.sectorCanvas(),
              startAngle: sdeg >= 90 ? sdeg - 90 : sdeg + 270,
              endAngle: edeg >= 90 ? edeg - 90 : edeg + 270,
              startRadius: sradius,
              endRadius: eradius
            });
          }
          return layer;
        },

        //经纬度范围是否正确
        checkLatLngs: function(latlngs){
          var iscorrect = true;
          for (var i = 0, len = latlngs.length; i < len; i++){
            var latlng = latlngs[i];
            if (latlng.lat > 90 || latlng.lat < -90 || latlng.lng > 180 || latlng.lng < -180){
              iscorrect = false;
              break;
            }
          }
          return iscorrect;
        },

        // 经纬度是否越界（经度范围：-180-180；纬度范围：-90-90）
        isLatLngsOutExtend: function (bounds){
           var res = false;
           var minlng, maxlng, minlat, maxlat;
           minlng = bounds.getWest();
           maxlng = bounds.getEast();
           minlat = bounds.getSouth();
           maxlat = bounds.getNorth();
           if (Math.abs(maxlng - minlng) >= 360 || Math.abs(maxlat - minlat) >= 180){
             res = true;
           }
           return res;
        }

    });

});
